package com.levy.dao.impl;

import java.util.List;

import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.entity.Entity;
import org.nutz.dao.entity.Record;
import org.nutz.dao.sql.Sql;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.levy.dao.UserDao;
import com.levy.model.User;

@Repository
public class UserDaoImpl implements UserDao {

	@Autowired
	private Dao dao;
	
	@Override
	public User getUserById(int userId) {
		String sqlString = "select user_id as userId,sex,name,time from test_user where user_id = @userId";
		Sql sql = Sqls.fetchEntity(sqlString);
		Entity<User> entity = dao.getEntity(User.class);
		sql.setEntity(entity).params().set("userId", userId);
		dao.execute(sql);
		return sql.getObject(User.class);
	}

	@Override
	public User getUserByCreateTime(String time) {
		String sqlString = "select user_id as userId,sex,name,time from test_user where time >@time limit 1";
		Sql sql = Sqls.queryRecord(sqlString);
		sql.params().set("time", time);
		this.dao.execute(sql);
		List<Record> rs = sql.getList(Record.class);
		if (rs!=null&&rs.size()>0) {
			User user = new User();
			user.setUserId(Integer.parseInt(rs.get(0).getString("userId")));
			user.setSex(rs.get(0).getString("sex"));
			user.setName(rs.get(0).getString("name"));
			user.setTime(rs.get(0).getString("time"));
			return user;
		}else{
			return null;
		}
		
	}

}
